package model

import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"time"
)

var Log *log.Logger
var OfflineLog *log.Logger

func init() {
	logFile, err := os.OpenFile("./log_info.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalln("open log file failed", err)
	}
	//日志
	Log = log.New(io.MultiWriter(logFile), "【LOG】:", log.Ldate|log.Ltime|log.Lshortfile)

	OfflineLogFile, err := os.OpenFile("./OffOnline.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalln("open log file failed", err)
	}
	//日志
	OfflineLog = log.New(io.MultiWriter(OfflineLogFile), "", log.Ldate|log.Ltime)
}

//清除离线记录
func CleanOffLine() {
	//ticker := time.NewTicker(time.Second * 5)
	ticker := time.NewTicker(time.Minute * 1440 * 30)
	for _ = range ticker.C {
		wg.Lock()
		OffOnline, _ := ioutil.ReadFile("OffOnline.log")
		if err := ioutil.WriteFile("./history/OffOnline_"+time.Now().Format("2006_01_02_15_04_05")+".log", OffOnline, 0644); err != nil {
			Log.Println(err.Error())
		}
		if err := os.Truncate("./OffOnline.log", 0); err != nil {
			Log.Println(err.Error())
		}
		wg.Unlock()
	}
}
